package com.snda.sc;

import java.nio.file.Path;
import java.nio.file.Paths;

import javax.servlet.ServletContext;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

@Component("pathInitProcessor")
public class PathInitProcessor implements BeanFactoryPostProcessor, ServletContextAware {

	private ServletContext servletContext;

	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		String appLocation = servletContext.getRealPath("/");
		Path appRootDir = Paths.get(appLocation);

		beanFactory.registerSingleton("appRootDir", appRootDir);
		beanFactory.registerSingleton("globalConfDir", appRootDir.getParent().resolve("conf"));
		beanFactory.registerSingleton("appConfDir", appRootDir.resolve("WEB-INF/conf"));
	}
}
